package com.yypx.entity;

import com.yypx.common.utils.StringUtil;

public class PageBean extends BaseObject {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int totalResults; // 总记录数
	private int pageSize = 10; // 每页显示的记录数
	private int currentPage = 1; // 当前页号
	private String urlString;// 链接
	private int totalPageSize;// 总页数，计算得到

	public PageBean(int totalResults, String pageSize, String currentPage, String urlString) {
		super();
		this.totalResults = totalResults;
		if (StringUtil.isStringHasContent(currentPage)) {
			try {
				setCurrentPage(Integer.parseInt(currentPage));
			} catch (Exception e) {
				setCurrentPage(1);
			}
		}
		if (StringUtil.isStringHasContent(pageSize)) {
			try {
				setPageSize(Integer.parseInt(pageSize));
			} catch (Exception e) {
				setPageSize(10);
			}
		}
		setUrlString(urlString);
		// 计算总页数
		setTotalPageSize((int) Math.ceil(this.totalResults / (double) this.pageSize));
	}

	/**
	 * 
	 * @return 得到总记录数
	 */
	public int getTotalResults() {
		return totalResults;
	}

	public void setTotalResults(int totalResults) {
		this.totalResults = totalResults;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		if (this.currentPage < 1) {
			this.currentPage = 1;
		}
		if (this.currentPage > this.totalResults) {
			this.currentPage = this.totalResults;
		}
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 是否有下一页
	 * 
	 * @return
	 */
	public boolean IsNextPage() {
		if (this.currentPage < this.getTotalPageSize()) {
			return true;
		}
		return false;
	}

	/**
	 * 是否有上一页
	 */
	public boolean IsUpPage() {
		if (this.currentPage > 1) {
			return true;
		}
		return false;
	}

	/**
	 * 从哪一条开始取
	 */
	public int getStartResult() {
		return (this.currentPage - 1) * this.pageSize;
	}

	public int getEndResult() {
		return Math.min(this.currentPage * this.pageSize, this.totalResults);
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("<div class='navbar'>");
		// 是否首页
		if (IsUpPage()) {
			sb.append("<a href='" + urlString + "?page=1'><<</a>&nbsp;");
			sb.append("<a href='" + urlString + "?page=" + (currentPage - 1) + "'>上一页</a>&nbsp;");
		}
		sb.append(currentPage + "&nbsp;");
		// 是否有下一页
		if (IsNextPage()) {
			sb.append("<a href='" + urlString + "?page=" + (currentPage + 1) + "'>下一页</a>&nbsp;");
			sb.append("<a href='" + urlString + "?page=" + totalPageSize + "'>>></a>&nbsp;");
		}
		sb.append("&nbsp;&nbsp;页数:" + currentPage + "/" + totalPageSize + "&nbsp;&nbsp;");
		sb.append("记录总数:" + totalResults);
		sb.append("</div>");
		return sb.toString();
	}

	public void setUrlString(String urlString) {
		this.urlString = urlString;
	}

	public String getUrlString() {
		return urlString;
	}

	public void setTotalPageSize(int totalPageSize) {
		this.totalPageSize = totalPageSize;
	}

	public int getTotalPageSize() {
		return totalPageSize;
	}
}
